<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>咸鱼云 - 缓存管理</title>
    <link rel="stylesheet/less" href="/src/css/xiaotao-ui.less">
    <link rel="stylesheet" href="/src/css/base.css">
    <script src="/src/dep/js/less.min.js"></script>
    <script src="/src/dep/js/vue.js"></script>
    <script src="/src/js/base.js"></script>
    <script src="/src/js/xiaotao-ui.js"></script>
    <style>
        html,body{
            background: none;
            height: auto;
        }
    </style>
</head>
<body>
    <div id="app">
        <xt-card title="缓存管理">
            <p>索引创建日期：{{formatDate(new Date(stat.res_cache_info.last_make_cache*1000))}}</p>
            <p>索引更新日期：{{formatDate(new Date(stat.res_cache_info.last_update_cache*1000))}}</p>
            <p>索引状态：{{stat.res_cache_info.cache_stat == 'making' ? '构建进行中...' : stat.res_cache_info.cache_stat}}</p>
            <p>文件总大小：{{getSizeString(stat.res_cache_info.total_file_size)}}</p>
            <p>文件数：{{stat.res_cache_info.file_count}} 目录数：{{stat.res_cache_info.dir_count}}</p>
            <xt-btn @click="updateCache">更新缓存</xt-btn>
            <xt-btn @click="makeCache">重建缓存（谨慎！）</xt-btn>
        </xt-card>
    </div>
    <script>
        app = new Vue({
            el:"#app",
            data:{
                stat:{
                    res_cache_info:{last_make_cache:0,total_file_size:0,dir_count:0,file_count:0}
                }
            },
            created(){
                this.getInfo()
            },
            methods: {
                getInfo(){
                    aoej.load()
                    $ajax("post","/api/admin/Resource/getCacheStat",null,(e)=>{
                        aoej.finish()
                        if(e.status != 200 || e.data.status != 1){
                            this.alert("出错","<br>响应码：" + e.status + "<br>状态码：" + e.data.status + "<br>其他信息：" + e.data.msg)
                        }else{
                            this.stat = e.data.data
                        }
                    })
                }
                ,
                updateCache() {
                    this.confirm("提示","确定要更新索引吗？",{
                        callback:e=>{
                            if(e){
                                this.execBuildCache()
                            }
                        }
                    })
                },
                execBuildCache(){
                    aoej.load()
                    $ajax('post','/api/admin/Resource/updateCache',null,(e)=>{
                        aoej.finish()
                        if(e.status != 200 || e.data.status != 1){
                            this.confirm("出错","响应码：" + e.status + "<br>状态码：" + e.data.status + "<br>其他信息：" + e.data.msg + "<br>是否重试？",{callback:e=>{
                                if(e) this.execBuildCache()
                            }})
                        }else{
                            this.alert("提示","更新完成，耗时：" + e.data.data.time + " <br>新增缓存：" + e.data.data.new + "<br>删除无效缓存：" + e.data.data.remove,{callback:()=>{this.getInfo()}})
                        }
                    })
                },makeCache(){
                    this.confirm("警告","这将可能会耗费大量的时间创建缓存，搜索功能将不可用，确定继续？",{
                        callback:e=>{
                            if(e){
                                this.confirm("警告","你确定！？？",{callback:e=>{
                                    if(e){
                                        this.confirm("警告","来真的！！？？服务器可能会爆炸喔！！！",{callback:e=>{
                                            this.confirm("最后一次警告了！！","是否继续重建缓存？",{callback:e=>{
                                                if(e){
                                                    this.execMakeCache()
                                                }
                                            }})
                                        }})
                                    }
                                }})
                            }
                        }
                    })
                },execMakeCache(){
                    aoej.load()
                    $ajax('post','/api/admin/Resource/makeCache',null,(e)=>{
                        aoej.finish()
                        if(e.status != 200 || e.data.status != 1){
                            this.alert("出错","响应码：" + e.status + "<br>状态码：" + e.data.status + "<br>其他信息：" + e.data.msg)
                        }else{
                            this.alert("提示","更新完成，耗时：" + formatDate(new Date(e.data.data.time*1000)) + " <br>文件数：" + e.data.data.file_count + "<br>目录数：" + e.data.data.dir_count + "<br>总大小：" + getSizeString(e.data.total_file_size),{callback:()=>{this.getInfo()}})
                        }
                    })
                }
            },
        })
    </script>
</body>
</html>
